package DAO;

import utility.HibernateUtil;
import java.util.List;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import model.Project;
import model.User;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import validator.e;

public class ProjectDAO extends BaseDAO<Project> {

    @Override
    public void save(Project project) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        FacesContext fc = FacesContext.getCurrentInstance();
        HttpSession sessions = (HttpSession) fc.getExternalContext().getSession(false);

        if (consult(project.getName())) {
            try {
                session.save(project);
                t.commit();
                e.showMsgSuccess("Sucess");
                sessions.setAttribute("display", true);
            } catch (HibernateException err) {
                e.showMsgError("Error - Try Again");
                sessions.setAttribute("display", false);
            }
        } else {
            e.showMsgError("Error - Duplicate Name");
            sessions.setAttribute("display", false);
        }
        session.close();
    }

    @Override
    public List<Project> list() {
        FacesContext fc = FacesContext.getCurrentInstance();
        HttpSession sessions = (HttpSession) fc.getExternalContext().getSession(false);
        User u = (User) sessions.getAttribute("user");
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        Query query = session.createSQLQuery("select * from Project p where p.user_id= :user_id").addEntity(Project.class);
        query.setParameter("user_id", u.getId());
        List lista = query.list();
        session.close();
        return lista;
    }

    @Override
    public void update(Project entity) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void remove(Project entity) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean consult(String name) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        FacesContext fc = FacesContext.getCurrentInstance();
        HttpSession sessions = (HttpSession) fc.getExternalContext().getSession(false);
        User u = (User) sessions.getAttribute("user");
        Query query = session.createSQLQuery("select * from Project where name = :name and user_id = :user_id").addEntity(Project.class);
        query.setParameter("name", name);
        query.setParameter("user_id", u.getId());
        List lista = query.list();
        if (lista == null || lista.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }
}
